blog

Home / DeveloperSection / Blogs / JSON Syntax

JSON Syntax

Anchal Kesharwani3841 22-Sep-2014

In this blog, I’m explaining the syntax rules for JSON.

 

JSON is the JavaScript Object Notation that is used for data store and interchanging the data.

There are following syntax to create JSON:

JSON Data Syntax

JSON data syntax for name and values. In the JSON how to give data name and the value? JSON Data gives data name as double code and value also but not necessary to every case:

Syntax

                “data_name”:”data_value”

Example

                “name”:”Amit Singh”

JSON values does not allow all values to double that means it’s define how value is integer or Boolean, etc.

There are following JSON value can accept:

·    A number may be integer or float that is not in the double codes.

·    A string use the double code.

·    A Boolean can accept the true or false without double codes.

·    An array define under the square brackets ‘[]’.

·     null is not to give any value.

Create JSON Objects

To create the JSON objects use the curly braces ’{}’. It accept the multi values for the object. Here, an example of student that contain the name, age, and employee or not and phone number.

Example
                {“name”:”Ajay Mishra”,”age”:21, employer:false, ”phone”:”121 123567”}
Try this example to understand it:
 

<!DOCTYPE html>

<html>

<body>

 

<h2>Create JSON Object in JavaScript</h2>

<p id="student"></p>

 

 

<script>

var student = '{"name":"Ajay Mishra","age":21,"address":"Allahabad","phone":"555 1234567","email":"ajay@mail.com"}';

 

var obj = JSON.parse(student);

 

document.getElementById("student").innerHTML =

"<b>Name </b>"+obj.name + "<br>" +

"<b>Age </b>"+obj.age +"<br>" +

"<b>Address </b>"+obj.address + "<br>" +

"<b>Phone </b>"+obj.phone+"<br>"+

"<b>E-mail </b>"+obj.email;

</script>

 

</body>

 

</html>

The JSON Object is given output as student

JSON Syntax

In this example name is the string, age is integer, and employer is Boolean that all types defined values. 

Create JSON Arrays

Arrays works normally other programming languages. Arrays created in the square brackets.

Example
  “students”:[
                    {“name”:”Ajay Mishra”,”age”:21, employer:false, ”phone”:”121 123567”},
             {“name”:”Satish Kumar”,”age”:21, employer:true, ”phone”:”121 123568”},
               {“name”:”Amit Singh”,”age”:21, employer:false, ”phone”:”121 123569”}
];

 

In the above example, the ‘students’ is an array object that contain the three JSON objects that contain all student information as name, age employer and the phone number. 

JSON uses the JavaScript Syntax

JSON use the JavaScript syntax. The JSON that not need to any extra software it use only the JavaScript.

With the JavaScript you can create an array objects as like this:

 

Example
    var students = [
                  {“name”:”Ajay Mishra”,”age”:21, employer:false, ”phone”:”121 123567”},
                {“name”:”Satish Kumar”,”age”:21, employer:true, ”phone”:”121 123568”},
           {“name”:”Amit Singh”,”age”:21, employer:false, ”phone”:”121 123569”}
];

This example as similar to JSON syntax but little difference. 

Create JSON Files

JSON file create with the extension of .json that contain mime type with the “application/json”.

             

Updated 22-Sep-2014

Leave Comment

Comments

Liked By